
using Application.Allot_dService;
using Application.Allot_hService;
using Application.BusinessTypeService;
using Application.Check_dService;
using Application.Check_hService;
using Application.CodeRuleService;
using Application.CustomerService;
using Application.DashboardService;
using Application.DepartmentService;
using Application.DictDataService;
using Application.DictTypeService;
using Application.InfoService;
using Application.Instock_dService;
using Application.Instock_hService;
using Application.ISale_hService;
using Application.Outstock_dService;
using Application.Outstock_hService;
using Application.PermissionService;
using Application.PositionService;
using Application.ProductImgService;
using Application.ProductService;
using Application.ProductTypeService;
using Application.ProductUnitService;
using Application.Profiles;
using Application.PublicFileService;
using Application.Purchase_dService;
using Application.Purchase_hService;
using Application.PurchaseReturn_dService;
using Application.PurchaseReturn_hService;
using Application.RolepermissionService;
using Application.RoleService;
using Application.Sale_dService;
using Application.SaleReturn_dService;
using Application.SaleReturn_hService;
using Application.SlotServic;
using Application.StockService;
using Application.StockWarningService;
using Application.SupplierService;
using Application.UserRoleService;
using Application.UserService;
using Application.WarehouseService;
using Domain.Entity.sys;
using Domain.IRepository;
using EFCore;
using EFCore.Repository;
using Hangfire;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Text;
using WMSManage.Hubs;

namespace WMSManage
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen(option =>
            {  // Swagger 注释功能
                string basePath = AppContext.BaseDirectory; // 获取应用程序所在目录（绝对路径）
                string xmlPath = Path.Combine(basePath, "WMSManage.xml");
                option.IncludeXmlComments(xmlPath);
            });

            {
                //注册redis缓存
                builder.Services.AddStackExchangeRedisCache(options =>
                {
                    options.Configuration = "localhost";
                    options.InstanceName = "cache_";//避免混乱
                });
            }


            {
                //注册实体映射转换
                builder.Services.AddAutoMapper(typeof(Profie));

                //注册上下文服务
                //builder.Services.AddDbContext<MyDbContext>();

                //注入仓储服务
                builder.Services.AddScoped<IUserRepository, UserRepository>();
                builder.Services.AddScoped<IPermissionRepository, PermissionRepository>();
                builder.Services.AddScoped<ICodeRuleRepository, CodeRuleRepository>();
                builder.Services.AddScoped<IBusinessTypeRepository, BusinessTypeRepository>();
                builder.Services.AddScoped<IDictTypeRepository, DictTypeRepository>();
                builder.Services.AddScoped<IDictDataRepository, DictDataRepository>();
                builder.Services.AddScoped<IProductTypeRepository, ProductTypeRepository>();
                builder.Services.AddScoped<IProductRepository, ProductRepository>();
                builder.Services.AddScoped<IWarehouseRepository, WarehouseRepository>();
                builder.Services.AddScoped<IDepartmentRepository, DepartmentRepository>();
                builder.Services.AddScoped<ISupplierRepository, SupplierRepository>();
                builder.Services.AddScoped<ICustomerRepository, CustomerRepository>();
                builder.Services.AddScoped<ISlotRepository, SlotRepository>();
                builder.Services.AddScoped<IProductImgRepository, ProductImgRepository>();
                builder.Services.AddScoped<IPublicFileRepository,PublicFileRepository>();
                builder.Services.AddScoped<IProductUnitRepository, ProductUnitRepository>();
                builder.Services.AddScoped<IPurchase_hRepository, Purchase_hRepository>();
                builder.Services.AddScoped<IPurchase_dRepository, Purchase_dRepository>();
                builder.Services.AddScoped<ISale_hRepository, Sale_hRepository>();
                builder.Services.AddScoped<ISale_dRepository, Sale_dRepository>();
                builder.Services.AddScoped<IPositionRepository, PositionRepository>();
                builder.Services.AddScoped<IInstock_hRepository, Instock_hRepository>(); 
                builder.Services.AddScoped<IInstock_dRepository, Instock_dRepository>();
                builder.Services.AddScoped<IUserRoleRepository, UserRoleRepository>();
                builder.Services.AddScoped<IRoleRepository, RoleRepository>();
                builder.Services.AddScoped<IStockRepository, StockRepository>();
                builder.Services.AddScoped<IRolePermissionRepository, RolePermissionRepository>();
                builder.Services.AddScoped<IOutstock_hRepository, Outstock_hRepository>();
                builder.Services.AddScoped<IAllot_hRepository, Allot_hRepository>();
                builder.Services.AddScoped<IAllot_dRepository, Allot_dRepository>();
                builder.Services.AddScoped<IOutstock_dRepository, Outstock_dRepository>();
                builder.Services.AddScoped<IPurchaseReturn_hRepository, PurchaseReturn_hRepository>();
                builder.Services.AddScoped<IPurchaseReturn_dRepository, PurchaseReturn_dRepository>();
                builder.Services.AddScoped<ISaleReturn_hRepository, SaleReturn_hRepository>();
                builder.Services.AddScoped<ISaleReturn_dRepository, SaleReturn_dRepository>();
                builder.Services.AddScoped<IStockWarningRepository, StockWarningRepository>();
                builder.Services.AddScoped<IInfoRepository, InfoRepository>();
                builder.Services.AddScoped<IDashboardRepository, DashboardRepository>();
                builder.Services.AddScoped<ICheck_hRepository, Check_hRepository>();
                builder.Services.AddScoped<ICheck_dRepository, Check_dRepository>();



                //注入操作服务
                builder.Services.AddScoped<IUserService, UserService>();
                builder.Services.AddScoped<IPermissionService, PermissionService>();
                builder.Services.AddScoped<ICodeRuleService, CodeRuleService>();
                builder.Services.AddScoped<IBusinessTypeService, BusinessTypeService>();
                builder.Services.AddScoped<IDictTypeService, DictTypeService>();
                builder.Services.AddScoped<IDictDataService, DictDataService>();
                builder.Services.AddScoped<IProductTypeService, ProductTypeService>();
                builder.Services.AddScoped<IProductService, ProductService>();
                builder.Services.AddScoped<IWarehouseService, WarehouseService>();
                builder.Services.AddScoped<IDepartmentService, DepartmentService>();
                builder.Services.AddScoped<ICustomerService, CustomerService>();
                builder.Services.AddScoped<ISupplierService, SupplierService>();
                builder.Services.AddScoped<ISlotService, SlotService>();
                builder.Services.AddScoped<IProductImgService, ProductImgService>();
                builder.Services.AddScoped<IPublicFileService, PublicFileService>();
                builder.Services.AddScoped<IProductUnitService, ProductUnitService>();
                builder.Services.AddScoped<IPurchase_hService, Purchase_hService>();
                builder.Services.AddScoped<IPurchase_dService, Purchase_dService>();
                builder.Services.AddScoped<ISale_hService, Sale_hService>();
                builder.Services.AddScoped<ISale_dService, Sale_dService>();
                builder.Services.AddScoped<IPositionService, PositionService>();
                builder.Services.AddScoped<IInstock_hService, Instock_hService>();
                builder.Services.AddScoped<IInstock_dService, Instock_dService>();
                builder.Services.AddScoped<IUserRoleService, UserRoleService>();
                builder.Services.AddScoped<IRoleService, RoleService>();
                builder.Services.AddScoped<IStockService, StockService>();
                builder.Services.AddScoped<IOutstock_hService, Outstock_hService>();
                builder.Services.AddScoped<IAllot_hService, Allot_hService>();
                builder.Services.AddScoped<IAllot_dService, Allot_dService>();
                builder.Services.AddScoped<IOutstock_dService, Outstock_dService>();
                builder.Services.AddScoped<IPurchaseReturn_hService, PurchaseReturn_hService>();
                builder.Services.AddScoped<IPurchaseReturn_dService, PurchaseReturn_dService>();
                builder.Services.AddScoped<ISaleReturn_hService, SaleReturn_hService>();
                builder.Services.AddScoped<ISaleReturn_dService, SaleReturn_dService>();
                builder.Services.AddScoped<IRolePermissionService, RolePermissionService>();
                builder.Services.AddScoped<IStockWarningService, StockWarningService>();
                builder.Services.AddScoped<IInfoService, InfoService>();
                builder.Services.AddScoped<IDashboardService, DashboardService>();
                builder.Services.AddScoped<ICheck_hService, Check_hService>();
                builder.Services.AddScoped<ICheck_dService, Check_dService>();
            }



            {
                //跨域
                builder.Services.AddCors(options =>
                {
                    options.AddPolicy("AllowAll", builder =>
                    {
                        builder.WithOrigins("http://localhost:8080")  // 允许任何来源（包括所有端口）
                               .AllowAnyMethod()  // 允许所有HTTP方法（GET/POST/PUT等）
                               .AllowAnyHeader() // 允许所有请求头
                               .AllowCredentials();
                    });
                });
            }

            {
                //授权认证
                builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                        .AddJwtBearer(option =>
                        {
                            option.TokenValidationParameters = new TokenValidationParameters
                            {
                                ValidateIssuerSigningKey = true,//是否验证SecurityKey
                                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SDMC-CJAS1-SAD-DFSFA-SADHJVF-VFSS")),
                                ValidateIssuer = true,//是否验证Issuer
                                ValidIssuer = "http://localhost:5178",
                                ValidateAudience = true,//是否验证Audience
                                ValidAudience = "http://localhost:5194",
                                ValidateLifetime = true,//是否验证失效时间

                            };
                        });
            }

            {
                //鉴权
                builder.Services.AddSwaggerGen(option =>
                {
                    //添加安全定义
                    option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                    {
                        Description = "请输入token,格式为 Bearer xxxxxxxx（注意中间必须有空格）",
                        Name = "Authorization",
                        In = ParameterLocation.Header,
                        Type = SecuritySchemeType.ApiKey,
                        BearerFormat = "JWT",
                        Scheme = "Bearer"
                    });
                    //添加安全要求
                    option.AddSecurityRequirement(new OpenApiSecurityRequirement {
                        {
                            new OpenApiSecurityScheme{
                                Reference =new OpenApiReference{
                                    Type = ReferenceType.SecurityScheme,
                                    Id ="Bearer"
                                }
                            },new string[]{ }
                        }
                    });
                });
            }


            //配置hangfire服务
            {
                builder.Services.AddHangfire(x => x.UseSqlServerStorage(@"Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False"));
                builder.Services.AddHangfireServer();//用于配置和运行作业。
            }

            {
                //配置SignalR服务
                builder.Services.AddSignalR();
            }



            var app = builder.Build();

            //Hangfire 仪表板 UI
            app.UseHangfireDashboard();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseStaticFiles();//启用静态文件
            app.UseCors("AllowAll");
            //完成鉴权和授权的中间件引用
            app.UseAuthentication(); //JWT 第一步 //鉴权---解析请求里面带的token得到需要的信息
            app.UseAuthorization(); //授权 ---根据用户信息来判断是否可以响应资源给请求

            app.MapHub<ChatHub>("/ChatHub");

            app.MapControllers();

            app.Run();
        }
    }
}
